package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.TravelingArticleDAO;
import bist.believeinstep.domain.TravelingArticle;
import bist.believeinstep.mapper.TravellingArticleMapper;

public class TravelingArticleTemplate implements TravelingArticleDAO, ConnectionDAO {
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(TravelingArticle travelingArticle){
		String query = "INSERT INTO bist_traveling_articles ("
				+ "title, content, introduction, type, avatar, state, created_date, modified_date,"
				+ "created_by, modified_by) VALUES (?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
				travelingArticle.getTitle(),
				travelingArticle.getContent(),
				travelingArticle.getIntroduction(),
				travelingArticle.getType(),
				travelingArticle.getAvatar(),
				travelingArticle.getState(),
				travelingArticle.getCreated(),
				travelingArticle.getModified(),
				travelingArticle.getCreatedBy(),
				travelingArticle.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(TravelingArticle travelingArticle){
		String query = "UPDATE bist_traveling_articles SET"
				+ "title = ?"
				+ "content = ?"
				+ "introduction = ?"
				+ "type = ?"
				+ "avatar = ?"
				+ "state = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "moodified_by = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				travelingArticle.getTitle(),
				travelingArticle.getContent(),
				travelingArticle.getIntroduction(),
				travelingArticle.getType(),
				travelingArticle.getAvatar(),
				travelingArticle.getState(),
				travelingArticle.getCreated(),
				travelingArticle.getModified(),
				travelingArticle.getCreatedBy(),
				travelingArticle.getModifiedBy()	
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public TravelingArticle getTravelingArticle(Integer id){
		String query = "SELECT FROM bist_traveling_articles WHERE id = ?";
		try{
			TravelingArticle travelingArticle = jdbcTemplate.queryForObject(query, new Object[]{id},
					new TravellingArticleMapper());
			return travelingArticle;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<TravelingArticle> list(){
		String query = "SELECT *  FROM bist_traveling_articles";
		try{
			List<TravelingArticle> travelingArticle = jdbcTemplate.query(query, new TravellingArticleMapper());
			return travelingArticle;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bis_traveling_articles WHERE id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
